package com.shoplook2025.good.api.serviceimpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shoplook2025.brand.model.Brand;
import com.shoplook2025.brand.service.BrandService;
import com.shoplook2025.category.model.Category;
import com.shoplook2025.category.service.CategoryService;
import com.shoplook2025.core.utils.utils.JsonResult;
import com.shoplook2025.good.api.dao.GoodsMapper;
import com.shoplook2025.good.api.service.BrandFeignService;
import com.shoplook2025.good.api.service.CategoryFeignService;
import com.shoplook2025.good.model.Goods;
import com.shoplook2025.good.model.search.GoodsSearchBean;
import com.shoplook2025.good.service.GoodsService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yan
 * @date 2025/8/26
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    private GoodsMapper goodsMapper;
    private CategoryFeignService  categoryFeignService;
    private BrandFeignService brandFeignService;
    @Autowired
    public void setCategoryFeignService(CategoryFeignService categoryFeignService) {
        this.categoryFeignService = categoryFeignService;
    }
    @Autowired
    public void setBrandFeignService(BrandFeignService brandFeignService) {
        this.brandFeignService = brandFeignService;
    }

    @Autowired
    public void setGoodsMapper(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }

    @Override
    public Page<Goods> findAll(Page<Goods> page, GoodsSearchBean gsb) {
        Page<Goods> result = goodsMapper.findAll(page,gsb);
        result.getRecords().forEach(this::patch);
        return result;
    }

    private void patch(Goods it) {
        ResponseEntity<JsonResult<Category>> resp = this.categoryFeignService.findById(it.getCategoryId());
        JsonResult<Category> jsonResult = resp.getBody();
        if(jsonResult!=null){
            it.setCategory(jsonResult.getData());
        }
        ResponseEntity<JsonResult<Brand>> resp1 = this.brandFeignService.findById(it.getBrandId());
        JsonResult<Brand> jsonResult1 = resp1.getBody();
        if(jsonResult1!=null){
            it.setBrand(jsonResult1.getData());
        }
    }

    @Override
    public Goods findById(Integer id) {
        Goods goods = goodsMapper.selectById(id);
        patch(goods);
        return goods;
    }

    @Override
    public int save(Goods goods) {
        return goodsMapper.insert(goods);
    }

    @Override
    public boolean update(Goods goods) {
        return goodsMapper.updateById(goods)>0;
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return goodsMapper.deleteByIds(ids);
    }

}
